package com.cvnert.testplatform.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cvnert.testplatform.domain.*;
import com.cvnert.testplatform.domain.vo.TestCaseListVo;
import com.cvnert.testplatform.domain.vo.TestCaseVo;
import com.cvnert.testplatform.mapper.*;
import com.cvnert.testplatform.service.TestCaseService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

/**
* @author pc
* @description 针对表【test_case】的数据库操作Service实现
* @createDate 2024-03-24 14:48:20
*/
@Service
public class TestCaseServiceImpl extends ServiceImpl<TestCaseMapper, TestCase> implements TestCaseService{
    @Resource
    private TestCaseMapper testCaseMapper;

    @Resource
    private TastCaseVersionMapper tastCaseVersionMapper;

    @Resource
    private UserMapper userMapper;

    @Resource
    private TaskTaskCaseMapper tastTastCaseMapper;

    @Resource
    private VersionMapper versionMapper;

    /**
     * 新增用例
     * @param testCaseVo
     * @return
     */
    @Override
    public Object addTestCase(TestCaseVo testCaseVo) {
        HashMap<String, Object> res = new HashMap<>();
        // 查看版本是否存在
        Version version = versionMapper.selectById(testCaseVo.getVid());
        if (version == null) {
            res.put("state", false);
            res.put("msg", "版本不存在！");
            return res;
        }else{
            // 新增测试用例
            TestCase testCase = new TestCase();
            testCase.setCasename(testCaseVo.getCasename());
            testCase.setCasedes(testCaseVo.getCasedes());
            testCase.setCasemain(testCaseVo.getCasemain());
            testCase.setIssuccess(testCaseVo.getIssuccess());
            testCase.setUid(testCaseVo.getUid());
            testCase.setCreatetime(LocalDateTime.now());

            TastCaseVersion tastCaseVersion1 = new TastCaseVersion();
            tastCaseVersion1.setVid(testCaseVo.getVid());
            tastCaseVersion1.setCid(testCase.getCid());

            TaskTaskCase tastTastCase = new TaskTaskCase();
            tastTastCase.setCid(testCase.getCid());
            tastTastCase.setTid(testCaseVo.getTid());

            tastTastCaseMapper.insert(tastTastCase);

            int insert = testCaseMapper.insert(testCase);
            if (insert == 0) {
                res.put("state", false);
                res.put("msg", "新增失败！");
                return res;
            }else{
                res.put("state", true);
                res.put("msg", "新增成功！");
                TestCase testCase1 = testCaseMapper.selectById(testCase.getId());
                testCase1.setCid(testCase1.getId());
                testCaseMapper.updateById(testCase1);

                TastCaseVersion tastCaseVersion = new TastCaseVersion();
                tastCaseVersion.setCid(testCase1.getCid());
                tastCaseVersion.setVid(testCaseVo.getVid());
                tastCaseVersionMapper.insert(tastCaseVersion);

                TaskTaskCase taskTaskCase = new TaskTaskCase();
                taskTaskCase.setCid(testCase1.getCid());
                taskTaskCase.setTid(testCaseVo.getTid());
                tastTastCaseMapper.insert(taskTaskCase);
                return res;
            }

        }
    }

    /**
     * 删除测试用例
     * @param cid
     * @return
     */
    @Override
    public Object deleteTestCase(Integer cid) {
        HashMap<String, Object> res = new HashMap<>();
        TestCase testCase = testCaseMapper.selectById(cid);
        if (testCase == null) {
            res.put("state", false);
            res.put("msg", "用例不存在！");
            return res;
        }else{
            testCaseMapper.deleteById(cid);
            res.put("state", true);
            res.put("msg", "删除成功！");
            return res;
        }
    }

    /**
     * 修改测试用例信息
     * @param testCaseVo
     * @return
     */
    @Override
    public Object updateTestCase(TestCaseVo testCaseVo) {
        // 查看版本是否存在
        HashMap<String, Object> res = new HashMap<>();
        Version version = versionMapper.selectById(testCaseVo.getVid());
        if (version == null) {
            return "版本不存在！";
        }else{
            // 修改测试用例
            TestCase testCase = new TestCase();
            testCase.setCid(testCaseVo.getCid());
            testCase.setId(testCaseVo.getCid());
            testCase.setCasename(testCaseVo.getCasename());
            testCase.setCasedes(testCaseVo.getCasedes());
            testCase.setCasemain(testCaseVo.getCasemain());
            testCase.setIssuccess(testCaseVo.getIssuccess());
            testCase.setUid(testCaseVo.getUid());
            testCase.setUpdatetime(LocalDateTime.now());
            testCaseMapper.updateById(testCase);
            res.put("state", true);
            res.put("msg", "修改成功！");
            return res;
        }
    }

    /**
     * 获取测试用例信息
     * @param testCaseListVo
     * @return
     */
    @Override
    public Object getTestCaseList(TestCaseListVo testCaseListVo) {
        HashMap<String, Object> res = new HashMap<>();
        if (testCaseListVo == null) {
            res.put("state", false);
            res.put("msg", "参数为空！");
            return res;
        }
        QueryWrapper<TaskTaskCase> wrapper1 = new QueryWrapper<>();
        wrapper1.eq("tid", testCaseListVo.getTid());
        List<TaskTaskCase> taskTaskCases = tastTastCaseMapper.selectList(wrapper1);
        taskTaskCases.remove(null);
        List<Integer> cidList = taskTaskCases.stream().map(TaskTaskCase::getCid).collect(Collectors.toList());
        if (cidList.size() == 0) {
            res.put("state", false);
            res.put("msg", "数据为空");
            return res;
        }
        // 获取任务下所有的用例 完成通过tid查询所有的case
        cidList.remove(null);

        Page<TestCase> cid = testCaseMapper.selectPage(new Page<>(testCaseListVo.getPageNum(), testCaseListVo.getPageSize()), new QueryWrapper<TestCase>().in("cid", cidList));


        QueryWrapper<TestCase> wrapper = new QueryWrapper<>();
        if (testCaseListVo.getCasename() != null) {
            wrapper.like("casename", testCaseListVo.getCasename());
        }
        if (testCaseListVo.getUid() != null) {
            wrapper.eq("uid", testCaseListVo.getUid());
        }
        if (testCaseListVo.getIssuccess() != null) {
            wrapper.eq("issuccess", testCaseListVo.getIssuccess());
        }
        Page<TestCase> testCasePage = testCaseMapper.selectPage(cid, wrapper);
        testCasePage.getRecords().forEach(item->{
            ArrayList<User> users = new ArrayList<>();
            Integer uid = item.getUid();
            User user = userMapper.selectById(uid);
            users.add(user);
            item.setUsers(users);
        });

        res.put("state", true);
        res.put("msg", "查询成功！");
        res.put("data", testCasePage);
        return res;
    }

    @Override
    public Object getTestCaseDetail(Integer cid) {
        HashMap<String, Object> res = new HashMap<>();
        TestCase testCase = testCaseMapper.selectById(cid);
        if (testCase == null) {
            res.put("state", false);
            res.put("msg", "用例不存在！");
            return res;
        }
        res.put("state", true);
        res.put("msg", "查询成功！");
        res.put("data", testCase);
        return res;
    }

    @Override
    public Object getDefectCount(TestCaseListVo testCaseListVo) {
        HashMap<String, Object> res = new HashMap<>();
        // 获取任务下所有的用例
        QueryWrapper<TaskTaskCase> wrapper = new QueryWrapper<>();
        if (testCaseListVo.getTid() == null) {
            res.put("state", false);
            res.put("msg", "参数为空！");
            return res;
        }
        wrapper.eq("tid", testCaseListVo.getTid());
        List<TaskTaskCase> taskTaskCases = tastTastCaseMapper.selectList(wrapper);
        res.put("state", true);
        res.put("msg", "查询成功！");
        res.put("data", taskTaskCases.size());
        return res;
    }


}




